package com.ymm.spark.spark.session;

import lombok.Data;
import scala.math.Ordered;

import java.io.Serializable;

/**
 * @author zhangj
 * @date 2019/11/6
 * 品类的自定义二次排序,必须序列化
 */
@Data
public class CategorySortKey implements Ordered<CategorySortKey>, Serializable {
	/**
	 * 点击次数
	 */
	private long clickCount;

	/**
	 * 下单次数
	 */
	private long orderCount;
	/**
	 * 支付次数
	 */
	private long payCount;

	public CategorySortKey(long clickCount, long orderCount, long payCount) {
		this.clickCount = clickCount;
		this.orderCount = orderCount;
		this.payCount = payCount;
	}

	@Override
	public boolean $less(CategorySortKey other) {
		if (clickCount < other.getClickCount()) {
			return true;
		} else if (clickCount == other.getClickCount() &&
				orderCount < other.getOrderCount()) {
			return true;
		} else if (clickCount == other.getClickCount() &&
				orderCount == other.getOrderCount() &&
				payCount < other.getPayCount()) {
			return true;
		}
		return false;
	}

	@Override
	public boolean $less$eq(CategorySortKey other) {
		if ($less(other)) {
			return true;
		} else if (clickCount == other.getClickCount() &&
				orderCount == other.getOrderCount() &&
				payCount == other.getPayCount()) {
			return true;
		}
		return false;
	}

	@Override
	public boolean $greater(CategorySortKey other) {
		if (clickCount > other.getClickCount()) {
			return true;
		} else if (clickCount == other.getClickCount() &&
				orderCount > other.getOrderCount()) {
			return true;
		} else if (clickCount == other.getClickCount() &&
				orderCount == other.getOrderCount() &&
				payCount > other.getPayCount()) {
			return true;
		}
		return false;
	}

	@Override
	public boolean $greater$eq(CategorySortKey other) {
		if ($greater(other)) {
			return true;
		} else if (clickCount == other.clickCount &&
				orderCount == other.orderCount &&
				payCount == other.payCount) {
			return true;
		}
		return false;
	}

	@Override
	public int compare(CategorySortKey other) {
		if (clickCount - other.getClickCount() != 0) {
			return (int) (clickCount - other.getClickCount());
		} else if (orderCount - other.getOrderCount() != 0) {
			return (int) (orderCount - other.getOrderCount());
		} else if (payCount - other.getPayCount() != 0) {
			return (int) (payCount - other.getPayCount());
		}
		return 0;
	}

	@Override
	public int compareTo(CategorySortKey other) {
		if (clickCount - other.getClickCount() != 0) {
			return (int) (clickCount - other.getClickCount());
		} else if (orderCount - other.getOrderCount() != 0) {
			return (int) (orderCount - other.getOrderCount());
		} else if (payCount - other.getPayCount() != 0) {
			return (int) (payCount - other.getPayCount());
		}
		return 0;
	}
}
